//
//  LKAddQrcodeViewController.m
//  Mideleague_Shop
//
//  Created by Zhiwang on 2020/7/15.
//  Copyright © 2020 Zhiwang. All rights reserved.
//

#import "LKAddQrcodeViewController.h"

@interface LKAddQrcodeViewController ()
@property(nonatomic,strong)LKBaseButton *addBtn;
@property(nonatomic,strong)UIImageView *qrImageView;
@property(nonatomic,strong)LKBaseButton *deleteBtn;

@end

@implementation LKAddQrcodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"添加客服二维码";
    [self setNavgationRightBtn];
    [self createHeaderIntroViewWithTile:@"客服二维码，最多上传1张" AndBottomIntro:@"(尺寸：600x600，大小：200KB,格式：JPG、JPEG、PNG)"];
    // Do any additional setup after loading the view.
}
-(void)setNavgationRightBtn{
    WS(weakSelf)
    LKBaseButton *rightBtn = [LKBaseButton buttonWithFrame:CGRectMake(0, 0, 64, 24) type:UIButtonTypeCustom title:@"保存" titleColor:COLOR_HEX(0x333333) layercorne:13 font:[UIFont fontWithName:@"PingFangSC-Regular" size: 16] backgroudColor:[UIColor clearColor] AndActionBlock:^(UIButton * _Nonnull btn) {
           NSMutableDictionary *param = [[NSMutableDictionary alloc] initWithDictionary:@{@"qrcode":weakSelf.qrImageUrl}];
            NSString *sign = [NSString encoingWithDic:param];
            [param setObject:sign forKey:@"sign"];
            [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:MyShopWechatUrl header:[LKUserInforTool account].token parameters:param progress:^(NSProgress * _Nullable progress) {
                
            } success:^(BOOL isSuccess, id  _Nullable responseObject) {
                [SVProgressHUD dismiss];
                if ([responseObject[@"code"] isEqualToString:@"success"]) {
                    if (weakSelf.block) {
                        weakSelf.block();
                    [weakSelf.navigationController popViewControllerAnimated:YES];
                    }

                }else{
                    [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
                }
            } failure:^(NSString * _Nullable errorMessage) {
                
            }];
        
    }];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
}
/// 增加客服二维码头部介绍
-(void)createHeaderIntroViewWithTile:(NSString *)title AndBottomIntro:(NSString *)intro{
    WS(weakSelf)
    LKBaseLabel *topLabel = [LKBaseLabel labelWithFrame:CGRectZero text:title textColor:COLOR_HEX(0x333333) font:[UIFont fontWithName:@"PingFangSC-Regular" size:14] textAlignment:NSTextAlignmentCenter];
    [self.view addSubview:topLabel];
    [topLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(NAVIGATION_BAR_HEIGHT + 10);
        make.left.equalTo(self.view.mas_left).offset(20);
    }];
    LKBaseLabel *bottomLabel = [LKBaseLabel labelWithFrame:CGRectZero text:intro textColor:COLOR_HEX(0x333333) font:[UIFont fontWithName:@"PingFangSC-Regular" size:12] textAlignment:NSTextAlignmentCenter];
    [self.view addSubview:bottomLabel];
    [bottomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topLabel.mas_bottom).offset(30);
        make.left.equalTo(self.view.mas_left).offset(20);
    }];
    self.addBtn = [LKBaseButton buttonWithFrame:CGRectMake(0, 0, 64, 24) type:UIButtonTypeCustom title:@"" titleColor:[UIColor whiteColor] layercorne:30 font:[UIFont fontWithName:@"PingFangSC-Regular" size: 24] backgroudColor:[UIColor lightGrayColor] AndActionBlock:^(UIButton * _Nonnull btn) {
        [weakSelf showAlertChooseVc];
    }];
    [self.addBtn setImage:[UIImage imageNamed:@"ic_add_photo"] forState:UIControlStateNormal];
    [self.view addSubview:self.addBtn];
    [self.addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view.mas_right).offset(-20);
        make.bottom.equalTo(self.view.mas_bottom).offset(-30);
        make.size.mas_equalTo(CGSizeMake(60, 60));
    }];
    self.qrImageView = [[UIImageView alloc] init];
    self.qrImageView.userInteractionEnabled = YES;
    [self.qrImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",ServerlUrl,[NSString judegeIsNullAndTransform:self.qrImageUrl]]] placeholderImage:[UIImage imageNamed:@""]];
    [self.view addSubview:_qrImageView];
    [_qrImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.centerY.equalTo(self.view);
        make.height.mas_equalTo((HEIGHT -NAVIGATION_BAR_HEIGHT)/2);
        
    }];
    self.deleteBtn = [LKBaseButton buttonWithFrame:CGRectMake(0, 0, 64, 24) type:UIButtonTypeCustom title:@"" titleColor:[UIColor whiteColor] layercorne:30 font:[UIFont fontWithName:@"PingFangSC-Regular" size: 24] backgroudColor:[UIColor lightGrayColor] AndActionBlock:^(UIButton * _Nonnull btn) {
        weakSelf.qrImageUrl = @"";
        weakSelf.qrImageView.hidden = YES;
        [weakSelf.qrImageView sd_setImageWithURL:[NSURL URLWithString:[NSString judegeIsNullAndTransform:self.qrImageUrl]] placeholderImage:[UIImage imageNamed:@""]];
          weakSelf.addBtn.hidden = NO;
    }];
    [self.deleteBtn setImage:[UIImage imageNamed:@"ic_del"] forState:UIControlStateNormal];
    [self.qrImageView addSubview:self.deleteBtn];
    [self.deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.qrImageView.mas_right).offset(-20);
        make.top.equalTo(self.qrImageView.mas_top).offset(20);
        make.size.mas_equalTo(CGSizeMake(60, 60));
    }];
    if (self.qrImageUrl.length != 0) {
        self.addBtn.hidden = YES;
    }else{
        self.qrImageView.hidden = YES;
    }
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info{
    WS(weakSelf)
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    if ([type isEqualToString:@"public.image"]) {

        UIImage *photo = [info objectForKey:UIImagePickerControllerOriginalImage];
//        row.selectImageV.superview.userInteractionEnabled = NO;
        weakSelf.qrImageView.image = photo;
        [SVProgressHUD showWithStatus:@"图片上传中..."];
        [[LKNetDataManager defaultManager] sendPOSTRequestWithserverUrl:ServerlUrl apiPath:ImageUploadUrl parameters:nil imageArray:@[weakSelf.qrImageView.image] targetWidth:100 progress:^(NSProgress * _Nullable progress) {
            
                } success:^(BOOL isSuccess, id  _Nullable responseObject) {
                    [SVProgressHUD dismiss];
                    if ([responseObject[@"code"] isEqualToString:@"success"]) {
                        if ([NSString isBlankString:responseObject[@"data"]]) {
                            [SVProgressHUD showInfoWithStatus:@"图片未上传成功，请重新选择"];
                            return ;
                        }
                        weakSelf.qrImageUrl = responseObject[@"data"];
                        weakSelf.qrImageView.hidden = NO;
                        weakSelf.addBtn.hidden = YES;
                        [weakSelf.qrImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",ServerlUrl,weakSelf.qrImageUrl]] placeholderImage:[UIImage imageNamed:@""]];
                    }else{
                       [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
                    }
            
                } failure:^(NSString * _Nullable error) {
            
                }];

        
    }
    [picker dismissViewControllerAnimated:YES completion:nil];
}

-(void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray<UIImage *> *)photos sourceAssets:(NSArray *)assets isSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto infos:(NSArray<NSDictionary *> *)infos{
    WS(weakSelf)
    weakSelf.qrImageView.image = photos[0];
    [SVProgressHUD showWithStatus:@"图片上传中..."];
    [[LKNetDataManager defaultManager] sendPOSTRequestWithserverUrl:ServerlUrl apiPath:ImageUploadUrl parameters:nil imageArray:@[self.qrImageView.image] targetWidth:100 progress:^(NSProgress * _Nullable progress) {
        
            } success:^(BOOL isSuccess, id  _Nullable responseObject) {
                [SVProgressHUD dismiss];
                if ([responseObject[@"code"] isEqualToString:@"success"]) {
                    if ([NSString isBlankString:responseObject[@"data"]]) {
                        [SVProgressHUD showInfoWithStatus:@"图片未上传成功，请重新选择"];
                        return ;
                    }
                    weakSelf.qrImageUrl = responseObject[@"data"];
                    weakSelf.qrImageView.hidden = NO;
                    weakSelf.addBtn.hidden = YES;
                    [weakSelf.qrImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",ServerlUrl,weakSelf.qrImageUrl]] placeholderImage:[UIImage imageNamed:@""]];
                }else{
                   [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
                }
        
            } failure:^(NSString * _Nullable error) {
        
            }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
